﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="HotKeyService.cs" company="WordSteps.com">
//   All rights reserved
// </copyright>
// <summary>
//   Defines the HotKeyService type.
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace RapidDict.Modules.HotKeyService.Services
{
    using System;
    using Infrastructure.Interfaces;
    using Utils;

    public class HotKeyService : IHotKeyService, IDisposable
    {
        private readonly IAppViewService appViewService;
        private readonly ITranslationService translationService;

        private HotKeyCatcher hotKeyCatcher;
        
        public HotKeyService(IAppViewService appViewService, ITranslationService translationService)
        {
            this.appViewService = appViewService;
            this.translationService = translationService;

            InitializeHotKeyCatcher();
        }

        private void InitializeHotKeyCatcher()
        {
            hotKeyCatcher = new HotKeyCatcher(appViewService, translationService);
            RegisterHotKey();
        }

        public void RegisterHotKey()
        {
            hotKeyCatcher.SetKeyboardHook();
        }

        public void UnregisterHotKey()
        {
            hotKeyCatcher.UnhookKeyboard();
        }

        ~HotKeyService()
        {
            Dispose();
            GC.SuppressFinalize(this);
        }

        public void Dispose()
        {
            UnregisterHotKey();
        }
    }
}